package org.example.six;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import org.example.utils.RabbitMqUtil;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

/**
 * Direct交换机
 * Direct交换机是最常用的交换机类型，它根据routing key将消息路由到与之匹配的队列中,注意,这里是队列中,01和02消费者在同一个队列。
 * 在使用Direct交换机时，需要在生产者发送消息时指定routing key，并在消费者监听队列时指定routing key。
 */
public class EmitLogDirect {
    public static final String EXCHANGE_NAME = "direct";

    public static void main(String[] args) throws IOException, TimeoutException {
        String routingKey = "error";
        Channel channel = RabbitMqUtil.getChannel();
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String message = scanner.next();
            channel.basicPublish(EXCHANGE_NAME,routingKey,null,message.getBytes());
            System.out.println("消息发送成功: "+message);
        }
    }
}
